<?php
/**
 * PhpStorm
 * User: zucheng
 * Date: 2019/5/6
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ShippingPay;
use Illuminate\Http\Request;

class ShippingPayController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $status = ShippingPay::$statusDesc;
        return view('admin.pay.index', compact('status'));
    }

    public function data(Request $request)
    {
        $model = ShippingPay::query();
        if ($request->get('start_time')) {
            $model = $model->where('created_at', '>=', $request->get('start_time'));
        }
        if ($request->get('end_time')) {
            $model = $model->where('created_at', '<=', $request->get('end_time'));
        }
        if ($request->get('shipping_no')) {
            $model = $model->where('s.shipping_no', $request->get('shipping_no'));
        }
        if ($request->get('status')) {
            $model = $model->where('v_shipping_pay.status', $request->get('status'));
        }
        $res = $model->leftJoin('v_shipping as s', 's.id', '=', 'v_shipping_pay.shipping_id')
            ->leftJoin('v_driver_bank as b', 'b.id', '=', 'v_shipping_pay.bank_id')
            ->select([
                'v_shipping_pay.*',
                's.shipping_no',
                'bank_card_no',
                'bank_branch',
                'driver_name',
                'driver_phone',
            ])
            ->orderBy('created_at', 'desc')
            ->paginate($request->get('limit', 30))
            ->toArray();

        if ($res['total'] > 0) {
            foreach ($res['data'] as $key => &$item) {
                $item['status_desc'] = ShippingPay::$statusDesc[$item['status']];
            }
        }

        return $this->table($res['data'], $res['total']);
    }

}